<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>消息推送表添加--${site.name}</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="description" content="${site.description}"/>
    <meta name="keywords" content="${site.keywords}"/>
    <meta name="author" content="${site.author}"/>
    <link rel="icon" href="${site.logo}">
    <link rel="stylesheet" href="${base}/static/layui/css/layui.css" media="all"/>
    <style type="text/css">
        .layui-form-item .layui-inline {
            width: 33.333%;
            float: left;
            margin-right: 0;
        }

        @media (max-width: 1240px) {
            .layui-form-item .layui-inline {
                width: 100%;
                float: none;
            }
        }

        .layui-form-item .role-box {
            position: relative;
        }

        .layui-form-item .role-box .jq-role-inline {
            height: 100%;
            overflow: auto;
        }
        .none{
            display: none;
        }

    </style>
</head>
<body class="childrenBody">
<form class="layui-form" style="width:80%;">
   <div class="layui-form-item" style="position: relative;z-index: 12008">
        <label class="layui-form-label">消息类型</label>
        <div class="layui-input-block">

            <select name="messageType" lay-verify="required" id="messageType" disabled="true">
                <option value="1" selected="">系统消息</option>
                <@my type="electricity_message_push_message_type">
                    <#list result as r>
                        <option value="${r.value}">${r.label}</option>
                    </#list>
                </@my>
            </select>
        </div>
    </div>
    <div class="layui-form-item" style="position: relative;z-index: 11008">
        <label class="layui-form-label">是否微信推送</label>
        <div class="layui-input-block">

            <select name="isWechat" lay-verify="required" lay-filter="isWechat" id="isWechat" disabled="true">
                <option value="2" selected="">否</option>
                <@my type="electricity_message_push_is_wechat">
                    <#list result as r>
                        <option value="${r.value}">${r.label}</option>
                    </#list>
                </@my>
            </select>
        </div>
    </div>
    <div class="layui-form-item noWxChat">
        <label class="layui-form-label">标题</label>
        <div class="layui-input-block">

            <input type="text" class="layui-input" name="title" id="title" placeholder="请输入标题">
        </div>
    </div>
    <#-- <div class="layui-form-item" noWxChat>
         <label class="layui-form-label">内容</label>
         <div class="layui-input-block">

             <div id="content"></div>
         </div>
     </div>-->

    <div class="layui-form-item wxChat" style="position: relative;z-index: 10008;display: none">
        <label class="layui-form-label">模板名称</label>
        <div class="layui-input-block">
            <select name="freemakerId" id="freemakerId" lay-filter="freemakerId">
                <option value="">请选择模板名称类型</option>
            </select>
        </div>
    </div>

    <#--    <div class="layui-form-item wxChat">-->
    <#--        <label class="layui-form-label">微信模板字段描述</label>-->
    <#--        <div class="layui-input-block"  id="content1" >-->
    <#--            <textarea class="form-control" name="content" id="content" ></textarea>-->
    <#--&lt;#&ndash;            <input  type="text"  class="layui-input" name="wxDesc"  placeholder="请输入微信模板字段描述">&ndash;&gt;-->
    <#--        </div>-->
    <#--    </div>-->

    <div class="layui-form-item noWxChat">
        <label class="layui-form-label">内容</label>
        <div class="layui-input-block">
            <div id="content" name="content"></div>
<#--            <textarea name="content" id="content" placeholder="请输入内容" class="layui-textarea">${electricityMessagePush.content}</textarea>-->
        </div>
    </div>
    <div class="layui-form-item wxChat" style="display: none">
        <label class="layui-form-label">消息推送描述</label>
        <div class="layui-input-block" id="wxDesc1">
            <textarea name="wxDesc" id="wxDesc" placeholder="请输入内容" class="layui-textarea"></textarea>
            <div class="wxDesc2">

            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="addElectricityMessagePush">保存草稿</button>
            <button class="layui-btn" lay-submit="" lay-filter="sendElectricityMessagePush">立即推送</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script type="text/javascript" src="${base}/static/layui/layui.js"></script>
<script type="text/javascript" src="${base}/static/js/wangEditor.min.js"></script>
<script type="text/javascript" src="${base}/static/js/libs/jquery-2.1.1.min.js"></script>
<script>
    layui.use(['form', 'jquery', 'layer', 'laydate'], function () {
        var form = layui.form,
            $ = layui.jquery,
            E = window.wangEditor,
            laydate = layui.laydate,
            layer = layui.layer;

        form.on('select(isWechat)', function (data) {
            var value = data.value;
            if (value == 2) {
                $(".noWxChat").show();
                $(".wxChat").hide();
            } else {
                $(".wxChat").show();
                $(".noWxChat").hide();
            }
        });

        form.on("select(freemakerId)", function (data) {
            $('.wxDesc2').empty()
            var loadIndex = layer.load(2, {
                shade: [0.3, '#333']
            });
            $.post("${base}/admin/electricityPushFreemakerDesc/queryMessagePushFormwordDesc", {"id": data.value}, function (res) {
                layer.close(loadIndex);
                if (res) {
                    if (res.length == 0)
                    {
                        $("#wxDesc1 textarea").removeClass('none');
                        $(".wxDesc2").addClass('none')
                    }else {
                        $("#wxDesc1 textarea").addClass('none');
                        $(".wxDesc2").removeClass('none')
                        for (var i = 0; i < res.length; i++) {
                            $(".wxDesc2").append(res[i].wxDesc + ':<input class="layui-input" name="' + res[i].wxDesc + '" type="text"/><input type="hidden" name="wxCode" value="' + res[i].wxCode + '" />');
                        }
                    }
                    // layui.form.render('textarea')
                }
            });
            return false;
        });

        //获取消息模板
        $.ajax({
            type: 'post',
            url: "${base}/admin/electricityPushFreemakerDesc/queryMessagePushFormwork",
            contentType: "application/json; charset=utf-8",
            async: false,
            success: function (data) {
                for (var i = 0; i < data.length; i++) {
                    $("#freemakerId").append("<option value='" + data[i].stringId + "'>" + data[i].freemakerName + "</option>");
                }
                layui.form.render('select')
            }
        });


        //初始赋值
        laydate.render({
            elem: '#pushDate'
        });
        var content_editor = new E('#content');
        //图片上传
        content_editor.customConfig.uploadImgServer = '${base}/file/uploadWang';
        content_editor.customConfig.uploadFileName = 'test';
        // 自定义处理粘贴的文本内容(这里处理图片抓取)
        content_editor.customConfig.pasteTextHandle = function (content) {
            if (undefined == content) {
                return content;
            }
            if (content.indexOf("src=") <= 0) {
                return content;
            }
            var loadContent = layer.load(2, {
                shade: [0.3, '#333']
            });
            $.ajax({
                url: "${base}/file/doContent/",
                type: "POST",
                async: false,
                data: {"content": content},
                dataType: "json",
                success: function (res) {
                    layer.close(loadContent);
                    content = res.data;
                }
            });
            return content;
        };
        // 关闭粘贴样式的过滤
        content_editor.customConfig.pasteFilterStyle = false;
        content_editor.customConfig.customAlert = function (info) {
            // info 是需要提示的内容
            layer.msg(info);
        };
        content_editor.create();


        form.on("submit(addElectricityMessagePush)", function (data) {
            var c = content_editor.txt.html();
            c = c.replace(/\"/g, "'");
            data.field.content = c;

            var inputList = $("#wxDesc1").find('input');
            console.log(inputList);
            var obj = {};
            for (var i = 0; i < inputList.length; i++) {
                var item = inputList[i];
                var name = $(item).attr('name');
                obj[name] = $(item).val();
            }
            var formdata = {};
            // formdata.messageType = $("#messageType").val();
            formdata.isWechat = $("#isWechat").val();
            formdata.freemakerId = $("#freemakerId").val();
            formdata.title = $("#title").val();
            formdata.wxDesc = $("#wxDesc").val();
            formdata.content = data.field.content;
            formdata.wxList = JSON.stringify(obj);
            console.log(JSON.stringify(formdata));
            var loadIndex = layer.load(2, {
                shade: [0.3, '#333']
            });
            $.post("${base}/admin/electricityMessagePush/add", {
                data: JSON.stringify(formdata),
                type: 2
            }, function (res) {
                layer.close(loadIndex);
                if (res.success) {
                    parent.layer.msg("消息推送表添加成功！", {time: 1000}, function () {
                        parent.layer.close(parent.addIndex);
                        //刷新父页面
                        parent.location.reload();
                    });
                } else {
                    layer.msg(res.message);
                }
            });
            return false;
        });

        form.on("submit(sendElectricityMessagePush)", function (data) {
            debugger;
            var c = content_editor.txt.html();
            c = c.replace(/\"/g, "'");
            data.field.content = c;

            var inputList = $("#wxDesc1").find('input');
            console.log(inputList);
            var obj = {};
            for (var i = 0; i < inputList.length; i++) {
                var item = inputList[i];
                var name = $(item).attr('name');
                obj[name] = $(item).val();
            }
            var formdata = {};
            // formdata.messageType = $("#messageType").val();
            formdata.isWechat = $("#isWechat").val();
            formdata.freemakerId = $("#freemakerId").val();
            formdata.title = $("#title").val();
            formdata.wxDesc = $("#wxDesc").val();
            formdata.content = data.field.content;
            console.log($("#content").val());
            formdata.wxList = JSON.stringify(obj);
            console.log(JSON.stringify(formdata));
            var loadIndex = layer.load(2, {
                shade: [0.3, '#333']
            });
            debugger;
            $.post("${base}/admin/electricityMessagePush/add", {
                data: JSON.stringify(formdata),
                type: 1
            }, function (res) {
                debugger;
                layer.close(loadIndex);
                if (res.success) {
                    parent.layer.msg("消息推送表添加成功！", {time: 1000}, function () {
                        parent.layer.close(parent.addIndex);
                        //刷新父页面
                        parent.location.reload();
                    });
                } else {
                    layer.msg(res.message);
                }
            });
            return false;
        });
    });
</script>
</body>
</html>